﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.SQlite;

namespace WindowsFormsApp1
{
    public partial class frmWorkHistory : Form
    {
        public frmWorkHistory()
        {
            InitializeComponent();
            tbWorkDescription.TextChanged += WorkDescriptionTextBox_TextChanged;
            tbCompany.TextChanged += CommonTextBox_TextChanged;
            tbDepartment.TextChanged += CommonTextBox_TextChanged;
            tbIndustry.TextChanged += CommonTextBox_TextChanged;
            tbPosition.TextChanged += CommonTextBox_TextChanged;
        }
        private void CommonTextBox_TextChanged(object sender, EventArgs e)
        {            
            TextBox textBox = sender as TextBox;
            if (textBox.Text.Length > 32)
            {
                textBox.Text = textBox.Text.Substring(0, 32);
            }

        }
        private void WorkDescriptionTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if(textBox.Text.Length > 200)
            {
                textBox.Text = textBox.Text.Substring(0, 200);
            }
            lbCharCount.Text = textBox.Text.Length.ToString() + "/200"; 

        }
        /// <summary>
        /// 工作经验的内容可以全部不填，那就忽略。要填必须全部都填
        /// </summary>
        /// <returns></returns>
        public int CheckInput()
        {
            if(tbCompany.Text.Trim() != string.Empty ||
                tbDepartment.Text.Trim() != string.Empty ||
                tbIndustry.Text.Trim() != string.Empty ||
                tbPosition.Text.Trim() != string.Empty ||
                tbWorkDescription.Text.Trim() != string.Empty )
            {
                //只要有一个不为空，就必须全部填
                if (tbCompany.Text.Trim() == string.Empty ||
                tbDepartment.Text.Trim() == string.Empty ||
                tbIndustry.Text.Trim() == string.Empty ||
                tbPosition.Text.Trim() == string.Empty ||
                tbWorkDescription.Text.Trim() == string.Empty)
                {
                    return -1;//工作经验只填了一部分
                }
                else
                {
                    return 1;//全部都填了，这种情况可以保存
                }
            }
            else
            {
                return 0;//全部为空
            }
        }
        public WorkHistoryModel getWorkHistory()
        {
            WorkHistoryModel whm = new WorkHistoryModel();
            whm.company = tbCompany.Text.Trim();
            whm.industy = tbIndustry.Text.Trim();
            whm.department = tbDepartment.Text.Trim();
            whm.endTime = dateTimePickerEnd.Value.ToString("yyyy-MM-dd");
            whm.position = tbPosition.Text.Trim();
            whm.startTime = dateTimePickerStart.Value.ToString("yyyy-MM-dd");
            whm.workDescription = tbWorkDescription.Text.Trim();
            return whm;
        }

        /// <summary>
        /// 清空界面所有值。一般用于新增简历时
        /// </summary>
        public void clearEmptyAllControl()
        {
            tbCompany.Text = "";
            tbDepartment.Text = "";
            tbIndustry.Text = "";
            tbPosition.Text = "";
            tbWorkDescription.Text = "";
            dateTimePickerStart.Value = DateTime.Now;
            dateTimePickerEnd.Value = DateTime.Now;

        }
        public void editWorkHistory(WorkHistoryModel model)
        {
            tbCompany.Text = model.company;
            tbDepartment.Text = model.department;
            tbIndustry.Text = model.industy;
            tbPosition.Text = model.position;
            tbWorkDescription.Text = model.workDescription;
            //string timeString = "2024-08-17 12:34:56";
            DateTime selectedDate;
            // 尝试解析时间字符串
            if (DateTime.TryParse(model.startTime, out selectedDate))
            {
                dateTimePickerStart.Value = selectedDate;
            }
            if (DateTime.TryParse(model.endTime, out selectedDate))
            {
                dateTimePickerEnd.Value = selectedDate;
            }
        }
    }
}
